/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbAdpTanx-1.0.0		*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_01			*/
/*	File:		GlbAdpTb.cpp			*/
/********************************************************/

#include "GlbAdpTanx.h"

bool g_bPrint = false;

void GlbAdpTanxCore(char* pszBody, int iLength)
{
	int iAds = 0;
	int iBody = 0;
	int iCount = 0;
	GLBATRES_C CRes;
	bool bReturn = false;
	char* pszHttp = pszBody;
	GLBATRESAD_C* pCAds = NULL;

	if (NULL != (pszBody = strstr(pszBody, GLB_AT_LENG))) {
		pszBody += strlen(GLB_AT_LENG);
		iBody = atoi(pszBody);
		if (true == g_bPrint) {
			GLB_PRINT("=== Http ===\n");
			GLB_PRINT("TanxLength: %d\n", iBody);
		}
		if (NULL != (pszBody = strstr(pszBody, GLB_AT_RNRN))) {
			*((pszBody += strlen(GLB_AT_RNRN)) - 1) = '\0';
			if (true == g_bPrint) {
				GLB_PRINT("RecvLength: %d\n%s", iLength, pszHttp);
			}
			bReturn = CRes.ParseFromArray(pszBody, iBody);
		}
		else {
			GLB_ERROR("Failed to Body\n");
		}
	}
	else {
		GLB_ERROR("Failed to Content\n");
	}

	if (true == bReturn) {
		// required int32 version = 1;
		if (true == CRes.has_version()) {
			if (true == g_bPrint) {
				GLB_PRINT("=== Tanx ===\n");
				std::cout << "version: " << CRes.version() << std::endl;
			}
		}
		else {
			if (true == g_bPrint) {
				std::cout << "version: NULL" << std::endl;
			}
		}

		// required string bid = 2;
		if (true == CRes.has_bid()) {
			if (true == g_bPrint) {
				std::cout << "bid: " << CRes.bid() << std::endl;
			}
  		}
		else {
			if (true == g_bPrint) {
				std::cout << "bid: NULL" << std::endl;
			}
		}

		// repeated Ads ads = 3;
		for (; iAds < CRes.ads_size(); iAds ++) {
			if (NULL != (pCAds = const_cast<GLBATRESAD_C*>(&CRes.ads(iAds)))) {
				// required uint32 adzinfo_id = 1;
				if (true == pCAds->has_adzinfo_id()) {
					if (true == g_bPrint) {
						std::cout << "{has_adzinfo_id: " << pCAds->adzinfo_id() << "} + ";
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{has_adzinfo_id: NULL} + ";
					}
				}

				// required uint32 max_cpm_price = 2;
				if (true == pCAds->has_max_cpm_price()) {
					if (true == g_bPrint) {
						std::cout << "{max_cpm_price: " << pCAds->max_cpm_price() << "} + ";
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{max_cpm_price: NULL} + ";
					}
				}

				// optional uint32 ad_bid_count_idx = 3;
				if (true == pCAds->has_ad_bid_count_idx()) {
					if (true == g_bPrint) {
						std::cout << "{ad_bid_count_idx: " << pCAds->ad_bid_count_idx() << "} + ";
					}
  				}
				else {
					if (true == g_bPrint) {
						std::cout << "{ad_bid_count_idx: NULL} + ";
					}
				}

				// optional string html_snippet = 4;
				if (true == pCAds->has_html_snippet()) {
					if (true == g_bPrint) {
						std::cout << "{html_snippet: " << pCAds->html_snippet() << "} + ";
					}
  				}
				else {
					if (true == g_bPrint) {
						std::cout << "{html_snippet: NULL} + ";
					}
				}

				// repeated string click_through_url = 5;
				if (pCAds->click_through_url_size() > 0) {
					for (iCount = 0; iCount < pCAds->click_through_url_size(); iCount ++) {
						if (true == g_bPrint) {
							std::cout << "{click_through_url: " << pCAds->click_through_url(iCount) << "} + ";
						}
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{click_through_url: NULL} + ";
					}
				}

				// repeated int32 category = 6;
				if (pCAds->category_size() > 0) {
					for (iCount = 0; iCount < pCAds->category_size(); iCount ++) {
						if (true == g_bPrint) {
							std::cout << "{category: " << pCAds->category(iCount) << "} + ";
						}
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{category: NULL} + ";
					}
				}

				// repeated int32 creative_type = 7;
				if (pCAds->creative_type_size() > 0) {
					for (iCount = 0; iCount < pCAds->creative_type_size(); iCount ++) {
						if (true == g_bPrint) {
							std::cout << "{creative_type: " << pCAds->creative_type(iCount) << "} + ";
						}
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{creative_type: NULL} + ";
					}
				}

				// optional string network_guid = 8;
				if (true == pCAds->has_network_guid()) {
					if (true == g_bPrint) {
						std::cout << "{network_guid: " << pCAds->network_guid() << "} + ";
					}
  				}
				else {
					if (true == g_bPrint) {
						std::cout << "{network_guid: NULL} + ";
					}
				}

				// optional string extend_data = 9;
				if (true == pCAds->has_extend_data()) {
					if (true == g_bPrint) {
						std::cout << "{extend_data: " << pCAds->extend_data() << "} + ";
					}
  				}
				else {
					if (true == g_bPrint) {
						std::cout << "{extend_data: NULL} + ";
					}
				}

				// repeated string destination_url = 10;
				if (pCAds->destination_url_size() > 0) {
					for (iCount = 0; iCount < pCAds->destination_url_size(); iCount ++) {
						if (true == g_bPrint) {
							std::cout << "{destination_url: " << pCAds->destination_url(iCount) << "} + ";
						}
					}
				}
				else {
					if (true == g_bPrint) {
						std::cout << "{destination_url: NULL} + ";
					}
				}

				// optional string creative_id = 11;
				if (true == pCAds->has_creative_id()) {
					if (true == g_bPrint) {
						std::cout << "{creative_id: " << pCAds->creative_id() << "}\n";
					}
  				}
				else {
					if (true == g_bPrint) {
						std::cout << "{creative_id: NULL}" << std::endl;
					}
				}
			}
		}
	}
	else {
		GLB_ERROR("Failed to ParseFromArray\n");
	}

	return;
}

int main(int argc, char* argv[])
{
	int iRecv = 0;
	int iTotal = 1;
	int iCount = 0;
 	int iIndex = 0;
	int iReturn = 0;
	int iSend = 450;
	GlbNet::CGlbTcp CTcp;
	GlbCls::CGlbTime CTime;
	char szUrl[GLB_URL] = "127.0.0.1:20201";
	struct option stOptions[] = {
		{"total", 1, 0, 't'},
		{"print", 0, 0, 'p'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0},
	};
	UCHAR uszRecv[GLB_PACKET] = { 0 };
	UCHAR uszSend[GLB_PACKET] = {
		0x50, 0x4f, 0x53, 0x54, 0x20, 0x2f, 0x74, 0x62, 0x69, 0x64, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50,
		0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41, 0x67, 0x65, 0x6e, 0x74,
		0x3a, 0x20, 0x4d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c, 0x61, 0x2f, 0x35, 0x2e, 0x30, 0x20, 0x28, 0x63,
		0x6f, 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x3b, 0x20, 0x54, 0x61, 0x6e, 0x78, 0x2f,
		0x31, 0x2e, 0x30, 0x3b, 0x20, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x29, 0x0d, 0x0a, 0x48, 0x6f, 0x73,
		0x74, 0x3a, 0x20, 0x74, 0x61, 0x6e, 0x78, 0x2e, 0x69, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x73,
		0x63, 0x68, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a,
		0x20, 0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
		0x70, 0x65, 0x3a, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6f,
		0x63, 0x74, 0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x0d, 0x0a, 0x43, 0x6f, 0x6e,
		0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x4b, 0x65, 0x65, 0x70, 0x2d, 0x41, 0x6c, 0x69,
		0x76, 0x65, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67,
		0x74, 0x68, 0x3a, 0x20, 0x32, 0x34, 0x37, 0x0d, 0x0a, 0x0d, 0x0a, 0x08, 0x03, 0x12, 0x20, 0x32,
		0x32, 0x30, 0x33, 0x62, 0x30, 0x32, 0x39, 0x35, 0x63, 0x31, 0x35, 0x65, 0x61, 0x64, 0x34, 0x61,
		0x39, 0x62, 0x62, 0x31, 0x30, 0x36, 0x39, 0x36, 0x38, 0x62, 0x66, 0x62, 0x63, 0x31, 0x64, 0x1a,
		0x0c, 0x4e, 0x32, 0x46, 0x68, 0x59, 0x54, 0x55, 0x33, 0x4f, 0x47, 0x45, 0x3d, 0x22, 0x0d, 0x31,
		0x30, 0x31, 0x2e, 0x36, 0x38, 0x2e, 0x38, 0x33, 0x2e, 0x32, 0x30, 0x35, 0x2a, 0x41, 0x4d, 0x6f,
		0x7a, 0x69, 0x6c, 0x6c, 0x61, 0x2f, 0x34, 0x2e, 0x30, 0x20, 0x28, 0x63, 0x6f, 0x6d, 0x70, 0x61,
		0x74, 0x69, 0x62, 0x6c, 0x65, 0x3b, 0x20, 0x4d, 0x53, 0x49, 0x45, 0x20, 0x36, 0x2e, 0x30, 0x3b,
		0x20, 0x57, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x73, 0x20, 0x4e, 0x54, 0x20, 0x35, 0x2e, 0x31, 0x3b,
		0x20, 0x53, 0x56, 0x31, 0x3b, 0x20, 0x54, 0x68, 0x65, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x29, 0x3a,
		0x2f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x65, 0x77, 0x73, 0x2e, 0x63, 0x6e, 0x2e,
		0x79, 0x61, 0x68, 0x6f, 0x6f, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6e, 0x65, 0x77, 0x73, 0x70, 0x69,
		0x63, 0x2f, 0x6e, 0x65, 0x77, 0x73, 0x2f, 0x32, 0x30, 0x34, 0x37, 0x35, 0x2f, 0x32, 0x30, 0x2f,
		0x40, 0xc9, 0xc6, 0x02, 0x48, 0x00, 0x52, 0x30, 0x08, 0x00, 0x12, 0x1b, 0x6d, 0x6d, 0x5f, 0x31,
		0x37, 0x32, 0x33, 0x30, 0x35, 0x37, 0x33, 0x5f, 0x32, 0x32, 0x37, 0x38, 0x36, 0x38, 0x34, 0x5f,
		0x39, 0x30, 0x37, 0x31, 0x30, 0x39, 0x31, 0x1a, 0x07, 0x33, 0x30, 0x30, 0x78, 0x32, 0x35, 0x30,
		0x20, 0x01, 0x28, 0x01, 0x38, 0x0a, 0x40, 0x01, 0x58, 0x01, 0x60, 0x00, 0x68, 0xe0, 0x03, 0x98,
		0x01, 0x01,
	};

	while (true) {
		iReturn = getopt_long(argc, argv, "t:ph", stOptions, &iIndex);
		if (iReturn < 0) {
			break;
		}
		switch (iReturn) {
			case 't':
				iTotal = atoi(optarg);
				break;
			case 'p':
				g_bPrint = true;
				break;
			case 'h':
				GLB_PRINT("Usage: %s -t100 -p\n", argv[0]);
				GLB_PRINT("[0] -t or --total:       set total.\n");
				GLB_PRINT("[0] -p or --print:       set print.\n");
				GLB_PRINT("[0] -h or --help:        show help.\n");
				return GLB_SUCCESS;
			default:
				return GLB_SUCCESS;
		}
	}

	CTime.GlbTimeOn();

	if (GLB_SUCCESS == CTcp.GlbTcpConnect(szUrl)) {
		CTcp.GlbNetSetTimeOut(0, 800000);
		for (; iCount < iTotal; iCount ++) {
			if (true == g_bPrint) {
				GLB_PRINT("\nCount[%d]:\n", iCount + 1);
			}
			if (CTcp.GlbNetSend(uszSend, iSend) > 0) {
				if ((iRecv = CTcp.GlbNetRecv(uszRecv, GLB_PACKET)) > 0) {
					GlbAdpTanxCore((char*)uszRecv, iRecv);
				}
				else {
					GLB_ERROR("Count[%08d]: Failed to GlbNetRecv\n", iCount);
				}
			}
			else {
				GLB_ERROR("Count[%08d]: Failed to GlbNetSend\n", iCount);
			}
		}
		CTcp.GlbNetClose();
	}
	else {
		GLB_ERROR("Failed to GlbTcpConnect\n");
	}

	GLB_PRINT("\nUse time ");
	CTime.GlbTimeOff();
	CTime.GlbTimeDisplay();

	return GLB_SUCCESS;
}
